package com.my.study;

import java.util.concurrent.RecursiveTask;

public class Fibonacci extends RecursiveTask<Integer> {

    private int n;

    public Fibonacci(int n) {
        this.n = n;
    }

    @Override
    protected Integer compute() {
        if(n <= 1){
            return n;
        }
        Fibonacci fibonacci = new Fibonacci(n-1);
        fibonacci.fork();
        Fibonacci fibonacci1 = new Fibonacci(n-2);
        return fibonacci1.compute() + fibonacci.join();
    }


    public static void main(String[] args) {


        Fibonacci fibonacci = new Fibonacci(4);
        System.out.println(fibonacci.compute());
    }
}

